package com.ccys.cms.service.administrator.impl;


import com.ccys.cms.service.administrator.TblDeliveryCountryLocationWebService;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.dto.administrator.TblDeliveryCountryLocationDTO;
import com.ccys.db.entity.TblDeliveryCountryLocation;
import com.ccys.db.mapper.TblDeliveryCountryLocationMapper;
import com.ccys.db.page.PageUtils;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.administrator.TblDeliveryCountryLocationWebVO;

import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * location-based 的delivery times lots管理
 */
@Service
@Slf4j
public class TblDeliveryCountryLocationWebServiceImpl extends BaseServiceImpl<TblDeliveryCountryLocation> implements TblDeliveryCountryLocationWebService {

    @Autowired(required = false)
    private TblDeliveryCountryLocationMapper tblDeliveryCountryLocationMapper;
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Override
    public ResponseResult listObjectsCms(HttpServletRequest request, HttpServletResponse response, TblDeliveryCountryLocationDTO dto) {
        PageUtils.startPage(request);
        List<TblDeliveryCountryLocationWebVO> list = this.tblDeliveryCountryLocationMapper.listObjectsCms(dto);
        PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }


    @Override
    public ResponseResult change(HttpServletRequest request, HttpServletResponse response, TblDeliveryCountryLocation entity) {
        if (StringUtils.isBlank(entity.getId())) {
            if(StringUtils.isEmpty(entity.getDcId())) {
                return ResponseResult.error("dcId is necessary");
            }
            if(entity.getDclStatus() == null) {
                entity.setDclStatus(1);
            }
            this.saveEntity(entity);
        } else { // 编辑
            this.updateEntityById(entity);
            if(entity.getDclStatus() != null) {
                this.baseTblAuditTrailWebService.saveStatusLog(entity,entity.getDclStatus());
            }
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult delete(HttpServletRequest request, HttpServletResponse response, TblDeliveryCountryLocation dto) {
        TblDeliveryCountryLocation entity = new TblDeliveryCountryLocation();
        entity.setId(dto.getId());
        entity.setDelFlag(1);
        this.updateEntityById(entity);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult listCity(HttpServletRequest request, HttpServletResponse response, TblDeliveryCountryLocationDTO dto) {
        //PageUtils.startPage(request);
        List<String> list = this.tblDeliveryCountryLocationMapper.listCity(dto);
        //PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(list);
    }

    @Override
    public ResponseResult listDistrict(HttpServletRequest request, HttpServletResponse response, TblDeliveryCountryLocationDTO dto) {
        //PageUtils.startPage(request);
        List<String> list = this.tblDeliveryCountryLocationMapper.listDistrict(dto);
        //PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(list);
    }

    @Override
    public ResponseResult listOtherDivision(HttpServletRequest request, HttpServletResponse response, TblDeliveryCountryLocationDTO dto) {
        //PageUtils.startPage(request);
        List<String> list = this.tblDeliveryCountryLocationMapper.listOtherDivision(dto);
        //PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(list);
    }


}